import 'package:flutter/material.dart';
import '../data/word_book_service.dart';
import '../models/word_card.dart';
import '../models/word_book.dart';

class TestPage extends StatefulWidget {
  const TestPage({Key? key}) : super(key: key);

  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  List<WordBook> _books = [];
  WordBook? _selectedBook;
  List<WordCard> _words = [];
  bool _isLoading = false;
  String _error = '';

  @override
  void initState() {
    super.initState();
    _loadBooks();
  }

  Future<void> _loadBooks() async {
    setState(() {
      _isLoading = true;
      _error = '';
    });

    try {
      final books = await WordBookService.getSampleBooks();
      setState(() {
        _books = books;
        _isLoading = false;
      });
    } catch (e) {
      setState(() {
        _error = '加载词书失败: $e';
        _isLoading = false;
      });
    }
  }

  Future<void> _loadWords() async {
    if (_selectedBook == null) {
      setState(() {
        _error = '请先选择词书';
      });
      return;
    }

    setState(() {
      _isLoading = true;
      _error = '';
      _words = [];
    });

    try {
      // 设置当前词书
      await WordBookService.setCurrentBook(_selectedBook!);
      
      // 获取词书总单词数
      final totalWords = await WordBookService.getBookTotalWords(_selectedBook!.id);
      
      // 加载速读模式的单词
      final words = await WordBookService.getSpeedReadingWords(_selectedBook!.id);
      
      setState(() {
        _words = words;
        _isLoading = false;
      });
      
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('成功加载 ${words.length} 个单词，词书总单词数: $totalWords')),
      );
    } catch (e) {
      setState(() {
        _error = '加载单词失败: $e';
        _isLoading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('单词加载测试'),
      ),
      body: _isLoading
          ? const Center(child: CircularProgressIndicator())
          : Column(
              children: [
                if (_error.isNotEmpty)
                  Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Text(
                      _error,
                      style: const TextStyle(color: Colors.red),
                    ),
                  ),
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: DropdownButton<WordBook>(
                    hint: const Text('选择词书'),
                    value: _selectedBook,
                    isExpanded: true,
                    items: _books.map((book) {
                      return DropdownMenuItem<WordBook>(
                        value: book,
                        child: Text(book.name),
                      );
                    }).toList(),
                    onChanged: (book) {
                      setState(() {
                        _selectedBook = book;
                      });
                    },
                  ),
                ),
                ElevatedButton(
                  onPressed: _loadWords,
                  child: const Text('加载单词'),
                ),
                const SizedBox(height: 16),
                Expanded(
                  child: _words.isEmpty
                      ? const Center(child: Text('暂无单词'))
                      : ListView.builder(
                          itemCount: _words.length,
                          itemBuilder: (context, index) {
                            final word = _words[index];
                            return ListTile(
                              title: Text(word.word),
                              subtitle: Text(word.meanCn),
                              trailing: Text(word.accent),
                            );
                          },
                        ),
                ),
              ],
            ),
    );
  }
} 